package sky.wicket;

import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;
import sky.domain.Country;
import sky.service.CountryService;
import sky.wicket.panels.CountryPanel;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Gauss
 * Date: 17.11.13
 * Time: 19:53
 * To change this template use File | Settings | File Templates.
 */
public class CountryList extends BasePage {

    @SpringBean
    CountryService countryService;
    List<Country> countryList;

    public CountryList() {
        countryList = countryService.list();
        addElementsToPage();
    }

    private void addElementsToPage() {
        //add airport link
        MyWicketUtils.addBookmarkablePageLinkWithLabel(this, "addCountryLink", AddUpdateCountry.class,
                "addCountryLabel", "Add Country");
        //list of flights
        ListView<Country> countryListView = new ListView<Country>("countryList", countryList) {
            @Override
            protected void populateItem(ListItem<Country> component) {
                final Country country = component.getModelObject();
                addComponentsToList(component, country);
            }
        };
        add(countryListView);
    }

    private void addComponentsToList(ListItem<Country> component, final Country country) {
        component.add(new CountryPanel("countryPanel", new Model<Country>(country)));
    }
}
